<?php
/**
 * The Class for the Widget.
 *
 * @package WP cr mobile
 */
class CRMobile_Widget extends WP_Widget
{
    /**
     * Register the Widget.
     */
    public function __construct()
    {
        $widget_ops = array(
            'classname' => 'widget_wp_cr_mobile',
            'description' => __('Displays WP cr mobile Reviews', 'wp-cr-mobile')
        );
        parent::__construct('wp_cr_mobile', 'WP cr mobile', $widget_ops);
    }

    /**
     * Front-end display of widget.
     *
     * @see WP_Widget::widget()
     *
     * @param array $args     Widget arguments.
     * @param array $instance Saved values from database.
     */
    public function widget($args, $instance)
    {
        extract($args);
        $title = apply_filters('widget_title', $instance['title'] );

        echo $before_widget;
        if (!empty( $title)) {
            echo $before_title . $title . $after_title;
        }

        $company = CRMobile_Shortcode::getCompanyById($instance['company']);
        $reviews = CRMobile_Shortcode::getReviews($company->ID, $instance['sort'], $instance['limit']);

        $data = array(
            'reviews' => $reviews,
            'star'    => plugins_url(
                "assets/stars/{$company->star}",
                CRMobile::FILE
            ),
        );

        echo CRMobile_View::render(
            plugin_dir_path(__FILE__).'../../views/widget-output.php',
            $data
        );

        echo $after_widget;
    }
    
    /**
     * Sanitize widget form values as they are saved.
     *
     * @see WP_Widget::update()
     *
     * @param array $new_instance Values just sent to be saved.
     * @param array $old_instance Previously saved values from database.
     *
     * @return array Updated safe values to be saved.
     */
    public function update($new_instance, $old_instance)
    {
        $instance = array();
        $instance['title'] = strip_tags($new_instance['title']);
        $instance['company'] = strip_tags($new_instance['company']);
        $instance['limit'] = strip_tags($new_instance['limit']);
        $instance['sort'] = strip_tags($new_instance['sort']);

        return $instance;
    }

    /**
     * Back-end widget form.
     *
     * @see WP_Widget::form()
     *
     * @param array $instance Previously saved values from database.
     */
    public function form($instance)
    {
        // Default Widget Settings
        $defaults = array(
            'title' => __('Reviews', 'wp-cr-mobile'),
            'company' => '',
            'limit' => 5,
            'sort'  => 'review_date'
        );
        $instance = wp_parse_args((array) $instance, $defaults);

        // Get list of companies
        global $wpdb;
        $company_table = $wpdb->prefix . "cr_mobile_companies";
        $query = "SELECT ID, name FROM $company_table";
        $companies = $wpdb->get_results($query, ARRAY_A);


        $data = array(
            'title'      => esc_attr($instance['title']),
            'title_id'   => $this->get_field_id('title'),
            'title_name' => $this->get_field_name('title'),

            'company'      => stripslashes($instance['company']),
            'company_id'   => $this->get_field_id('company'),
            'company_name' => $this->get_field_name('company'),
            'companies'    => $companies,

            'limit'      => esc_attr($instance['limit']),
            'limit_id'   => $this->get_field_id('limit'),
            'limit_name' => $this->get_field_name('limit'),

            'sort'      => esc_attr($instance['sort']),
            'sort_id'   => $this->get_field_id('sort'),
            'sort_name' => $this->get_field_name('sort'),
        );

        echo CRMobile_View::render(
            plugin_dir_path(__FILE__).'../../views/widget-form.php',
            $data
        );
    }
}
